﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySoft.Data;

namespace Caifu.Datas
{
	/// <summary>
	/// 基金净值预处
	/// </summary>
	public class NetValuePreData
	{
		/// <summary>
		/// 
		/// </summary>
		public static readonly NetValuePreData Instance = new NetValuePreData();

		private readonly DbSession dbSession = new DbSession("Caifu");

		/// <summary>
		/// 
		/// </summary>
		/// <param name="preFundNetValue"></param>
		public void AddNetValuePre(NetValuePre preFundNetValue)
		{
			preFundNetValue.Detach();
			if (preFundNetValue.Id == Guid.Empty) preFundNetValue.Id = Itfort.Comb.NewComb();
			if (preFundNetValue.AddTime == DateTime.MinValue) preFundNetValue.AddTime = DateTime.Now;
			if (preFundNetValue.UpdateTime == DateTime.MinValue) preFundNetValue.UpdateTime = DateTime.Now;
			dbSession.Save<NetValuePre>(preFundNetValue);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="preFundNetValue"></param>
		public void UpdateNetValuePre(NetValuePre preFundNetValue)
		{
			preFundNetValue.Attach();
			preFundNetValue.UpdateTime = DateTime.Now;
			dbSession.Save<NetValuePre>(preFundNetValue);
		}

		/// <summary>
		/// 更新来自网站1的数据
		/// </summary>
		/// <param name="unitNetValue">单位净值</param>
		/// <param name="accumulatedNetValue">累计净值</param>
		public void UpdateSite1Fields(string fundCode, decimal unitNetValue, decimal accumulatedNetValue)
		{

		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="id"></param>
		public void DeleteNetValuePre(Guid id)
		{
			dbSession.Delete<NetValuePre>(id);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public NetValuePre GetNetValuePre(Guid id)
		{
			return dbSession.Single<NetValuePre>(id);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="fundCode"></param>
		/// <param name="reportDate"></param>
		/// <returns></returns>
		public NetValuePre GetNetValuePre(string fundCode, DateTime reportDate)
		{
			var where = NetValuePre._.FundCode == fundCode && NetValuePre._.ReportDate == reportDate;

			return dbSession.Single<NetValuePre>(where);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="pageIndex"></param>
		/// <param name="pageSize"></param>
		/// <returns></returns>
		public IList<NetValuePre> GetNetValuePres(Int32 pageIndex, Int32 pageSize)
		{
			return dbSession.From<NetValuePre>()
				.OrderBy(NetValuePre._.ReportDate.Desc)
				.GetPage(pageSize).ToList(pageIndex);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="pageIndex"></param>
		/// <param name="pageSize"></param>
		/// <returns></returns>
		public IList<NetValuePre> GetNetValuePres(Guid fundId, Int32 pageIndex, Int32 pageSize)
		{
			var where = NetValuePre._.FundId == fundId;

			return dbSession.From<NetValuePre>()
				.Where(where)
				.OrderBy(NetValuePre._.ReportDate.Desc)
				.GetPage(pageSize).ToList(pageIndex);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="pageIndex"></param>
		/// <param name="pageSize"></param>
		/// <returns></returns>
		public IList<NetValuePre> GetNetValuePres(DateTime reportDate, Int32 pageIndex, Int32 pageSize)
		{
			var where = NetValuePre._.ReportDate == reportDate;

			return dbSession.From<NetValuePre>()
				.Where(where)
				.OrderBy(NetValuePre._.ReportDate.Desc)
				.GetPage(pageSize).ToList(pageIndex);
		}
	}
}

